// 数量加减，以及加减后价格的变化
addNum('.shop-list .chooseup','.default')
reduceNum('.shop-list .choosedown','.default')
function getPrice(){
    let sumPrice=0
    let sumNum=0
    $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.default').each(function(i,v){
        let sum=+$(v).text()
        sumNum+=sum
    })
    $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.item-sum').each(function(i,v){
        let sum=+$(v).text()
        sumPrice+=sum
    })
    $('.shop-content .sum-num').text(sumNum)
    $('.shop-content .sum-price').text(sumPrice.toFixed(2))
}

$('.shop-list .chooseup').click(
    function(){
        let price=+$(this).siblings('.shop-price').text()
        let num=+$(this).siblings('.default').text()
        let sum=price*num
        $(this).siblings('.item-sum').text(sum.toFixed(2))
        getPrice()
        /* let sumPrice=0
        let sumNum=0
        $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.default').each(function(i,v){
            let sum=+$(v).text()
            sumNum+=sum
        })
        $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.item-sum').each(function(i,v){
            let sum=+$(v).text()
            sumPrice+=sum
        })
        $('.shop-content .sum-num').text(sumNum)
        $('.shop-content .sum-price').text(sumPrice.toFixed(2)) */
    }
)
$('.shop-list .choosedown').click(
    function(){
        let price=+$(this).siblings('.shop-price').text()
        let num=+$(this).siblings('.default').text()
        let sum=price*num
        $(this).siblings('.item-sum').text(sum.toFixed(2))
        getPrice()
        /* let sumPrice=0
        let sumNum=0
        $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.default').each(function(i,v){
            let sum=+$(v).text()
            sumNum+=sum
        })
        $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.item-sum').each(function(i,v){
            let sum=+$(v).text()
            sumPrice+=sum
        })
        $('.shop-content .sum-num').text(sumNum)
        $('.shop-content .sum-price').text(sumPrice.toFixed(2)) */
        
    }
)
// 选中后商品的价格
$('.shop-list .choose-btn').click(function(){
    /* let sumPrice=0
    let sumNum=0
    $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.default').each(function(i,v){
        let sum=+$(v).text()
        sumNum+=sum
    })
    $('.shop-list .choose-btn:checked').closest('.shop-left').siblings().find('.item-sum').each(function(i,v){
        let sum=+$(v).text()
        sumPrice+=sum
    })
    $('.shop-content .sum-num').text(sumNum)
    $('.shop-content .sum-price').text(sumPrice.toFixed(2)) */
    getPrice()
    //如果选中全部则全选为选中状态，不是全部全选则取消全选
    let len1=$('.shop-list .choose-btn:checked').length
    let len2=$('.shop-list .choose-btn').length
    if(len1===len2){
        $('.sel-all').prop('checked',true)
    }else{
        $('.sel-all').prop('checked',false)
    }
})
//点击删除按钮，删除当前列
$('.shop-list .del-btn').on('click',function(){
    $(this).closest('.shop-list').remove()
    getPrice()
    let len1=$('.shop-list .choose-btn:checked').length
    let len2=$('.shop-list .choose-btn').length
    if(len1===len2){
        $('.sel-all').prop('checked',true)
    }else{
        $('.sel-all').prop('checked',false)
    }
    let len=$('.shop-ul').children().length
    if(len===0){
        $('.sel-all').prop('checked',false)
    }
})
// 点击删除选中产品，删除已经选中,当列表为空时，如果是全选，则取消全选
$('.del-product').on('click',function(){
    $('.shop-list .choose-btn:checked').closest('.shop-list').remove()
    getPrice()
    let len=$('.shop-ul').children().length
    if(len===0){
        $('.sel-all').prop('checked',false)
    }
})

// 点击全选按钮，单次全选，双次反选
let flag=true
$('.sel-all').on('click',function(){
    if(flag){
        $('.shop-list .choose-btn').prop('checked',true)
        $('.sel-all').prop('checked',true)
    }else{
        $('.shop-list .choose-btn').prop('checked',false)
        $('.sel-all').prop('checked',false)
    }
    flag=!flag
   
})